package com.example.crmweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.crmweb.entity.Customer;
import com.example.crmweb.entity.Staff;
import com.example.crmweb.lang.Result;
import com.example.crmweb.mapper.CustomerMapper;
import com.example.crmweb.mapper.StaffMapper;
import com.example.crmweb.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crmweb.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author -CRM-
 * @since 2022-03-18
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Resource
    CustomerMapper customerMapper;
    @Resource
    StaffService staffService;

    /**
     * @param customer  客户录入信息
     * @param staffName 员工名字
     * @return 返回200 成功  字符串 失败
     */
    @Override
    public Result insert(Customer customer, String staffName) {
        if (customer.getPhone() != null && customer.getName() != null) {
            //使用客户电话验重方法
            if (checkPhone(customer.getPhone()) >= 1) {
                return Result.fail("该客户电话号码已存在");
            }
            //调用员工库存减少方法
            Result reserve = staffService.reserve(staffName);
            if (reserve.getData() != null) {
                int inserts = customerMapper.insert(customer);
                if (inserts > 0) {
                    return Result.success(200, "添加客户信息成功", inserts);
                }
            }
            return Result.fail(400,"您的私库库存已满",null);
        }
        return Result.fail("请输入必填输入框");
    }

    /**
     * 客户修改
     *
      * @param customer_id 客户id
     * @param customer    修改的客户信息
     * @return result
     */
    @Override
    public Result update(Integer customer_id, Customer customer) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("id", customer_id);
        //第一个参数客户类里面包含修改的信息  第二参数寻找符合条件的客户类
        int update = customerMapper.update(customer, wrapper);
        if (update != 0) {
            return Result.success(200, "修改成功", null);
        }
        return Result.fail("修改错误");

    }

    /**
     * 客户电话号码验重
     *
     * @param phone 客户电话号码
     * @return 重复的电话
     */
    @Override
    public Integer checkPhone(String phone) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        return customerMapper.selectCount(wrapper);
    }

    /**
     * 查询所有的客户信息
     * @return 显示客户全部信息
     */
    @Override
    public Result SelectList() {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        List<Customer> customers = customerMapper.selectList(wrapper);
        return Result.success(200,"客户全部信息",customers);
    }

    /**
     * 模糊查询客户信息
     * @param name 客户的模糊名字
     * @return 类似的客户列表
     */
    @Override
    public Result fuzzy(String name) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.like("name",name);
        List<Customer> customers = customerMapper.selectList(wrapper);
        return Result.success(200,"查询成功",customers);
    }

    /**
     * 根据客户id进行查询客户信息
     * @param id 客户id
     * @return Result
     */
    @Override
    public Result SelectId(Integer id) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Customer customer = customerMapper.selectById(wrapper);
        return Result.success(200,"获取信息",customer);

    }


}
